<?php
/**
 * Plugin class is used to assign the error handler to current module
 *
 * It inherits from Zend_Controller_Plugin_Abstract
 *
 * @package Gcs_Controller
 * @subpackage Gcs_Controller_Plugin
 * @see Zend_Controller_Plugin_Abstract
 * @version $Id:$
 */
class Custom_Controller_Plugin_ModuleError extends Zend_Controller_Plugin_Abstract
{
    /**
     * Called after Zend_Controller_Router exits.
     *
     * Called after Zend_Controller_Front exits from the router.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();
        $errorHandler = Zend_Controller_Front::getInstance()
                        ->getPlugin('Zend_Controller_Plugin_ErrorHandler')
                        ->setErrorHandlerModule($module);
                        
        $front = Zend_Controller_Front::getInstance();
        if (!($front->getPlugin('Zend_Controller_Plugin_ErrorHandler') 
                      instanceof Zend_Controller_Plugin_ErrorHandler)) {
            return;
        }
        $error = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');
        $testRequest = new Zend_Controller_Request_Http();
        $testRequest->setModuleName($request->getModuleName())
            ->setControllerName($error->getErrorHandlerController())
            ->setActionName($error->getErrorHandlerAction());
        if ($front->getDispatcher()->isDispatchable($testRequest)) {
            $error->setErrorHandlerModule($request->getModuleName());
        }
    }
}